<?php
!defined('P_W') && exit('Forbidden');

class PW_House_BrokerDB extends BaseDB {
	var $_tableName = "pw_house_broker";
	var $_secondpostTableName = "pw_house_secondpost";
	var $_primaryKey = 'uid';
	
	/**
	 * 搜索经纪人
	 * @param array $param	搜索条件
	 * @param int $page		页面
	 * @param int $perPage	每页显示数
	 * @return array
	 */
	function findAll($param=array(),$page=1,$perPage=20,$order='') {
		$page = (int) $page;
		$page<1 && $page = 1;		
		$_sqlSearch = $this->_getSearchSQL($param,'b');
		if ($order && in_array($order,array('salenum','hirenum','createtime'))) {
			$order = ($order == 'createtime' ? 'b' : 's') . ".$order";
			$_sqlSearch .= " ORDER BY $order DESC ";
		}
		$_sql = 'SELECT b.*,s.salenum,s.hirenum,s.todaysalenum,s.lastsaletime,s.todayhirenum,s.lasthiretime FROM '.$this->_tableName." b LEFT JOIN ".$this->_secondpostTableName." s ON b.uid=s.uid $_sqlSearch".pwLimit(($page-1)*$perPage,$perPage);
		$query = $this->_db->query($_sql);
		$result = array();
		while ($rt = $this->_db->fetch_array($query)) {
			$result[] = $this->_unserializeData($rt);
		}
		return $result;
	}
	/**
	 * 获取数据个数
	 * @param array $param	搜索条件
	 */
	function count($param=array()) {
		$_sqlSearch = $this->_getSearchSQL($param);
		$_sql = 'SELECT COUNT(*) AS count FROM '.$this->_tableName." $_sqlSearch";
		return $this->_db->get_value($_sql);
	}
	
	/**
	 * 获取单条数据
	 */
	function get($uid) {
		$temp = $this->_get($uid);
		return $this->_unserializeData($temp);
	}
	/**
	 * 更新某条数据
	 * @param $fieldData
	 * @param $groupid
	 */
	function update($fieldData,$uid) {
		$fieldData = $this->_serializeData($fieldData);
		return $this->_update($fieldData,$uid);
	}
	/**
	 * 批量更新经纪人信息
	 * @param $fieldData
	 * @param $uids
	 */
	function updateMuti($fieldData,$uids){
		if(!$fieldData || !$uids || !S::isArray($fieldData) || !S::isArray($fieldData)){
			return false;
		}
		$sql = "UPDATE " . $this->_tableName . " SET " . $this->_getUpdateSqlString($fieldData) . " WHERE " . $this->_primaryKey . " IN (" . $this->_getImplodeString($uids).")";
		$this->_db->update($sql);
		return true;
	}
	/**
	 * 删除一条数据
	 * @param $groupid
	 */
	function delete($uid) {
		return $this->_delete($uid);
	}
	/**
	 * 添加一条数据
	 * @param $fieldData
	 */
	function insert($fieldData) {
		$fieldData = $this->_serializeData($fieldData);
		return $this->_insert($fieldData);
	}
	function getStruct() {
		return array('uid', 'username','groupid','realname','area','plate','company','address','email','tel','phone','verify','idcard','idcardpic','alipay','otherinfo','remark','createtime','salenum','hirenum','todaysalenum','lastsaletime','todayhirenum','lasthiretime');
	}
	
	
	function _serializeData($array) {
		if(!S::isArray($array))	return $array;
		if (isset($array['plate'])) $array['plate'] = $this->_serialize($array['plate']);
		return $array;
	}
	
	function _unserializeData($array) {
		if(!S::isArray($array)) return $array;
		if ($array['plate']) $array['plate'] = $this->_unserialize($array['plate']);
		return $array;
	}
	
	/**
	 * sql 组装器
	 * @param array $param
	 * @return string
	 */
	function _getSearchSQL($param,$tableMark = '') {
		if (!S::isArray($param)) return '';
		
		$temp = array();
		if ($param['username']) {
			$temp[] = $this->_getTableMark($tableMark).'username LIKE '.pwEscape($param['username'].'%');
			unset($param['username']);
		}
		if ($param['realname']) {
			$temp[] = $this->_getTableMark($tableMark).'realname LIKE '.pwEscape($param['realname'].'%');
			unset($param['realname']);
		}
		foreach ($param as $key=>$value) {
			if (!$value)	continue;
			if (!in_array($key,$this->getStruct())) continue;
			$temp[] = $this->_getTableMark($tableMark)."$key=".pwEscape($value);
		}
		return $temp ? 'WHERE '.implode(' AND ',$temp) : '';
	}
	
	function _getTableMark($tableMark) {
		if (!preg_match('/^[a-z]{1,2}$/',$tableMark)) return '';
		return $tableMark ? $tableMark.'.':'';
	}
}